
function testJson(){
    var obj = new Generic();
    obj.set('my_key', '1');
    obj.set('my_val', 'A');
    alert(obj.toJson());
                
    var lib = new DojoLib();
    lib.postJson('json_reciever.php', obj.toJson(), function(res){
        alert(res);
    });
}

function initialSomeTableGrid(){
    var lib = new DojoLib();
    lib.bindGridStore('services/retrieve_some_table.php', '', 'someTableGrid');
}

function openAddSomeTableDialog(){
    openSomeTableFormDialog('Add', 'Add', function(){}, function(){
        var obj = new Generic();
        obj.set('my_key', dijit.byId('keyTxt').get('value'));
        obj.set('my_val', dijit.byId('valTxt').get('value'));
        
        var lib = new DojoLib();
        lib.postJson('services/add_some_table.php', obj.toJson(), function(res){
            initialSomeTableGrid();
            dijit.byId('someTableFormDialog').hide();
        });
    });
}

function displayEditAndDeleteBtn(val){
    var html = '<a href="#" onClick="openEditSomeTableDialog(' + val + ');">Edit</a>&nbsp;';
    html += '<a href="#" onClick="deleteSomeTable(' + val + ');">Remove</a>&nbsp;';
    return html;
}

function deleteSomeTable(id){
    var obj = new Generic();
    obj.set('id', id);
        
    var lib = new DojoLib();
    lib.postJson('services/delete_some_table.php', obj.toJson(), function(res){
        initialSomeTableGrid();
    });
}

function openEditSomeTableDialog(id){
    openSomeTableFormDialog('Edit', 'Edit', function(){
        var obj = new Generic();
        obj.set('id', id);
        
        var lib = new DojoLib();
        lib.postJson('services/get_some_table.php', obj.toJson(), function(res){
            //alert(res);
            document.getElementById('idTxt').value = res.id;
            dijit.byId('keyTxt').set('value', res.my_key);
            dijit.byId('valTxt').set('value', res.my_val);
        });
    }, function(){
        var obj = new Generic();
        obj.set('id', document.getElementById('idTxt').value);
        obj.set('my_key', dijit.byId('keyTxt').get('value'));
        obj.set('my_val', dijit.byId('valTxt').get('value'));
        
        var lib = new DojoLib();
        lib.postJson('services/update_some_table.php', obj.toJson(), function(res){
            initialSomeTableGrid();
            dijit.byId('someTableFormDialog').hide();
        });
    });
}

function openSomeTableFormDialog(title, okBtnLabel, handleOnLoadDialog, handleOkBtn){
    var someTableFormDialog = new dijit.Dialog({
        title : title,
        id : "someTableFormDialog",
        style : "width:300px; height:140px;",
        href : "htmls/some_table_form.html",
        draggable : false,
        onHide : function() {
            this.destroyRecursive();
        },
        onLoad : function() {
            dijit.byId('okBtn').set('label', okBtnLabel);
            dijit.byId('okBtn').set('onClick', function(){
                handleOkBtn();
            });
            dijit.byId('cancelBtn').set('onClick', function(){
                someTableFormDialog.hide();
            });
            handleOnLoadDialog();
        }
    });
    someTableFormDialog.show();
}